import requests
from bs4 import BeautifulSoup
import lxml
import json

#网络请求
url = "http://data.sports.sohu.com/nba/nba_players_rank.php?season_year=2021&a=1&order_by=points&position=&division=&union=&stats=&advance=1&start=0"
headers = {
    'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}
resp = requests.get(url,headers=headers)
resp.encoding="GBK"
html = resp.text
soup = BeautifulSoup(html,"lxml")
taaa = ["排名","球员","球队","出场次数","场均时间","得分","命中/出手","命中率","命中/三分","三分命中率","命中/罚球","罚球命中率"]
table = soup.table
ls = []
trs = table.find_all("tr")
for tr in trs:
    tds = tr.find_all("td")
    map = {}
    i = 0
    for td in tds:
        map.update({taaa[i]:td.text})
        i += 1
    ls.append(map)
lss = json.dumps(ls,ensure_ascii=False)
fo = open("NBA.txt","w")
fo.write(lss)
fo.close()
print("完成")
